﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using nkstdgia.common.ThamDinhGia.BusinessObjects;

namespace nkstdgia.common.ThamDinhGia.DataAccess
{
    public class HO_SO_THAM_DINHDA
    {
        #region ***** Init Methods *****

        public HO_SO_THAM_DINHDA()
        {
        }

        #endregion ***** Init Methods *****

        #region ***** Get Methods *****

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public HO_SO_THAM_DINH Populate(IDataReader myReader)
        {
            HO_SO_THAM_DINH obj = new HO_SO_THAM_DINH();
            obj.MA_HO_SO = (Int64)myReader["MA_HO_SO"];
            obj.MA_DKS = (Int64)myReader["MA_DKS"];
            obj.SO_PHIEU = myReader["SO_PHIEU"] != DBNull.Value ? (string)myReader["SO_PHIEU"] : "";
            obj.TEN_CHU_SU_DUNG = myReader["TEN_CHU_SU_DUNG"] != DBNull.Value ? (string)myReader["TEN_CHU_SU_DUNG"] : "";
            obj.DIA_CHI = myReader["DIA_CHI"] != DBNull.Value ? (string)myReader["DIA_CHI"] : "";
            obj.GPS_LATITUDE = myReader["GPS_LATITUDE"] != DBNull.Value ? (double)myReader["GPS_LATITUDE"] : 0.0;
            obj.GPS_LONGTITUDE = myReader["GPS_LONGTITUDE"] != DBNull.Value ? (double)myReader["GPS_LONGTITUDE"] : 0.0;
            obj.GPS_CHI_CHU = myReader["GPS_CHI_CHU"] != DBNull.Value ? (string)myReader["GPS_CHI_CHU"] : "";
            obj.TINH_TRANG_PHAP_LY = (Int64)myReader["TINH_TRANG_PHAP_LY"];
            obj.NOI_DUNG_PHAP_LY = myReader["NOI_DUNG_PHAP_LY"] != DBNull.Value ? (string)myReader["NOI_DUNG_PHAP_LY"] : "";
            obj.LOAI_KHU_VUC_KINH_DOANH = (int)myReader["LOAI_KHU_VUC_KINH_DOANH"];
            obj.Y_KIEN_THAM_DINH = myReader["Y_KIEN_THAM_DINH"] != DBNull.Value ? (string)myReader["Y_KIEN_THAM_DINH"] : "";
            obj.GIA_DAT_1 = myReader["GIA_DAT_1"] != DBNull.Value ? (double)myReader["GIA_DAT_1"] : 0.0;
            obj.GIA_DAT_2 = myReader["GIA_DAT_2"] != DBNull.Value ? (double)myReader["GIA_DAT_2"] : 0.0;
            obj.GIA_DAT_BINH_QUAN_1 = myReader["GIA_DAT_BINH_QUAN_1"] != DBNull.Value ? (double)myReader["GIA_DAT_BINH_QUAN_1"] : 0.0;
            obj.GIA_DAT_BINH_QUAN_2 = myReader["GIA_DAT_BINH_QUAN_2"] != DBNull.Value ? (double)myReader["GIA_DAT_BINH_QUAN_2"] : 0.0;
            obj.NGUOI_KHOI_TAO = myReader["NGUOI_KHOI_TAO"] != DBNull.Value ? (Guid)myReader["NGUOI_KHOI_TAO"] : new Guid();
            obj.NGAY_KHOI_TAO = myReader["NGAY_KHOI_TAO"] != DBNull.Value ? (DateTime)myReader["NGAY_KHOI_TAO"] : new DateTime();
            obj.NGAY_CAP_NHAT = myReader["NGAY_CAP_NHAT"] != DBNull.Value ? (DateTime)myReader["NGAY_CAP_NHAT"] : new DateTime();
            obj.ACTIVE_FLAG = myReader["ACTIVE_FLAG"] != DBNull.Value ? (string)myReader["ACTIVE_FLAG"] : "N";
            return obj;
        }

        /// <summary>
        /// Get HO_SO_THAM_DINH by ma_ho_so
        /// </summary>
        /// <param name="ma_ho_so">MA_HO_SO</param>
        /// <returns>HO_SO_THAM_DINH</returns>
        public HO_SO_THAM_DINH GetByMA_HO_SO(Int64 ma_ho_so)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_HO_SO_THAM_DINH_GetByMA_HO_SO", Data.CreateParameter("MA_HO_SO", ma_ho_so)))
            {
                if (reader.Read())
                {
                    return Populate(reader);
                }
                return null;
            }
        }

        /// <summary>
        /// Get all of HO_SO_THAM_DINH
        /// </summary>
        /// <returns>List<<HO_SO_THAM_DINH>></returns>
        public List<HO_SO_THAM_DINH> GetList()
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_HO_SO_THAM_DINH_Get"))
            {
                List<HO_SO_THAM_DINH> list = new List<HO_SO_THAM_DINH>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of HO_SO_THAM_DINH
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet()
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_HO_SO_THAM_DINH_Get");
        }

        /// <summary>
        /// Get all of HO_SO_THAM_DINH paged
        /// </summary>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>List<<HO_SO_THAM_DINH>></returns>
        public List<HO_SO_THAM_DINH> GetListPaged(int recperpage, int pageindex)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_HO_SO_THAM_DINH_GetPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex)))
            {
                List<HO_SO_THAM_DINH> list = new List<HO_SO_THAM_DINH>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of HO_SO_THAM_DINH paged
        /// </summary>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSetPaged(int recperpage, int pageindex)
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_HO_SO_THAM_DINH_GetPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex));
        }

        #endregion ***** Get Methods *****

        #region ***** Add Update Delete Methods *****

        /// <summary>
        /// Add a new HO_SO_THAM_DINH within HO_SO_THAM_DINH database table
        /// </summary>
        /// <param name="obj">HO_SO_THAM_DINH</param>
        /// <returns>key of table</returns>
        public long Add(HO_SO_THAM_DINH obj)
        {
            DbParameter parameterItemID = Data.CreateParameter("MA_HO_SO", obj.MA_HO_SO);
            parameterItemID.Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_HO_SO_THAM_DINH_Add"
                            , parameterItemID
                            , Data.CreateParameter("MA_DKS", obj.MA_DKS)
                            , Data.CreateParameter("SO_PHIEU", obj.SO_PHIEU)
                            , Data.CreateParameter("TEN_CHU_SU_DUNG", obj.TEN_CHU_SU_DUNG)
                            , Data.CreateParameter("DIA_CHI", obj.DIA_CHI)
                            , Data.CreateParameter("GPS_LATITUDE", obj.GPS_LATITUDE)
                            , Data.CreateParameter("GPS_LONGTITUDE", obj.GPS_LONGTITUDE)
                            , Data.CreateParameter("GPS_CHI_CHU", obj.GPS_CHI_CHU)
                            , Data.CreateParameter("TINH_TRANG_PHAP_LY", obj.TINH_TRANG_PHAP_LY)
                            , Data.CreateParameter("NOI_DUNG_PHAP_LY", obj.NOI_DUNG_PHAP_LY)
                            , Data.CreateParameter("LOAI_KHU_VUC_KINH_DOANH", obj.LOAI_KHU_VUC_KINH_DOANH)
                            , Data.CreateParameter("Y_KIEN_THAM_DINH", obj.Y_KIEN_THAM_DINH)
                            , Data.CreateParameter("GIA_DAT_1", obj.GIA_DAT_1)
                            , Data.CreateParameter("GIA_DAT_2", obj.GIA_DAT_2)
                            , Data.CreateParameter("GIA_DAT_BINH_QUAN_1", obj.GIA_DAT_BINH_QUAN_1)
                            , Data.CreateParameter("GIA_DAT_BINH_QUAN_2", obj.GIA_DAT_BINH_QUAN_2)
                            , Data.CreateParameter("NGUOI_KHOI_TAO", obj.NGUOI_KHOI_TAO)
                            , Data.CreateParameter("NGAY_KHOI_TAO", obj.NGAY_KHOI_TAO)
                            , Data.CreateParameter("NGAY_CAP_NHAT", obj.NGAY_CAP_NHAT)
                            , Data.CreateParameter("ACTIVE_FLAG", obj.ACTIVE_FLAG)
            );
            return (long)parameterItemID.Value;
        }

        /// <summary>
        /// updates the specified HO_SO_THAM_DINH
        /// </summary>
        /// <param name="obj">HO_SO_THAM_DINH</param>
        /// <returns></returns>
        public void Update(HO_SO_THAM_DINH obj)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_HO_SO_THAM_DINH_Update"
                            , Data.CreateParameter("MA_HO_SO", obj.MA_HO_SO)
                            , Data.CreateParameter("MA_DKS", obj.MA_DKS)
                            , Data.CreateParameter("SO_PHIEU", obj.SO_PHIEU)
                            , Data.CreateParameter("TEN_CHU_SU_DUNG", obj.TEN_CHU_SU_DUNG)
                            , Data.CreateParameter("DIA_CHI", obj.DIA_CHI)
                            , Data.CreateParameter("GPS_LATITUDE", obj.GPS_LATITUDE)
                            , Data.CreateParameter("GPS_LONGTITUDE", obj.GPS_LONGTITUDE)
                            , Data.CreateParameter("GPS_CHI_CHU", obj.GPS_CHI_CHU)
                            , Data.CreateParameter("TINH_TRANG_PHAP_LY", obj.TINH_TRANG_PHAP_LY)
                            , Data.CreateParameter("NOI_DUNG_PHAP_LY", obj.NOI_DUNG_PHAP_LY)
                            , Data.CreateParameter("LOAI_KHU_VUC_KINH_DOANH", obj.LOAI_KHU_VUC_KINH_DOANH)
                            , Data.CreateParameter("Y_KIEN_THAM_DINH", obj.Y_KIEN_THAM_DINH)
                            , Data.CreateParameter("GIA_DAT_1", obj.GIA_DAT_1)
                            , Data.CreateParameter("GIA_DAT_2", obj.GIA_DAT_2)
                            , Data.CreateParameter("GIA_DAT_BINH_QUAN_1", obj.GIA_DAT_BINH_QUAN_1)
                            , Data.CreateParameter("GIA_DAT_BINH_QUAN_2", obj.GIA_DAT_BINH_QUAN_2)
                            , Data.CreateParameter("NGUOI_KHOI_TAO", obj.NGUOI_KHOI_TAO)
                            , Data.CreateParameter("NGAY_KHOI_TAO", obj.NGAY_KHOI_TAO)
                            , Data.CreateParameter("NGAY_CAP_NHAT", obj.NGAY_CAP_NHAT)
                            , Data.CreateParameter("ACTIVE_FLAG", obj.ACTIVE_FLAG)
            );
        }

        /// <summary>
        /// Delete the specified HO_SO_THAM_DINH
        /// </summary>
        /// <param name="ma_ho_so">MA_HO_SO</param>
        /// <returns></returns>
        public void Delete(Int64 ma_ho_so)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_HO_SO_THAM_DINH_Delete", Data.CreateParameter("MA_HO_SO", ma_ho_so));
        }

        #endregion ***** Add Update Delete Methods *****
    }
}